<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SpringBoot中实现动态数据源切换</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: attr(data-language);
            position: absolute;
            top: 8px;
            right: 12px;
            font-size: 12px;
            color: #858585;
            text-transform: uppercase;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .feature-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border: 1px solid #e5e7eb;
        }
        
        .feature-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 12px 30px rgba(0, 0, 0, 0.12);
            border-color: #667eea;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .nav-item {
            transition: all 0.3s ease;
            cursor: pointer;
            padding: 8px 16px;
            border-radius: 8px;
        }
        
        .nav-item:hover {
            background: rgba(102, 126, 234, 0.1);
            color: #667eea;
        }
        
        .sticky-nav {
            position: sticky;
            top: 20px;
            max-height: calc(100vh - 40px);
            overflow-y: auto;
        }
        
        .tech-badge {
            display: inline-flex;
            align-items: center;
            padding: 4px 12px;
            background: rgba(102, 126, 234, 0.1);
            color: #667eea;
            border-radius: 20px;
            font-size: 14px;
            margin: 4px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    SpringBoot中实现<br>动态数据源切换
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    构建高性能、可扩展的分布式数据库架构
                </p>
                <div class="flex flex-wrap justify-center gap-4">
                    <span class="tech-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-database mr-2"></i>多数据源
                    </span>
                    <span class="tech-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-sync-alt mr-2"></i>动态切换
                    </span>
                    <span class="tech-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-code-branch mr-2"></i>读写分离
                    </span>
                    <span class="tech-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-shield-alt mr-2"></i>事务管理
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="grid grid-cols-1 lg:grid-cols-4 gap-8">
            <!-- Sidebar Navigation -->
            <div class="lg:col-span-1">
                <div class="sticky-nav bg-white rounded-lg shadow-lg p-6">
                    <h3 class="text-lg font-bold mb-4 text-gray-800">
                        <i class="fas fa-compass mr-2 text-purple-600"></i>导航
                    </h3>
                    <nav class="space-y-2">
                        <div class="nav-item" onclick="scrollToSection('intro')">引言</div>
                        <div class="nav-item" onclick="scrollToSection('basics')">基础知识</div>
                        <div class="nav-item" onclick="scrollToSection('design')">设计思路</div>
                        <div class="nav-item" onclick="scrollToSection('implementation')">实现步骤</div>
                        <div class="nav-item" onclick="scrollToSection('strategy')">切换策略</div>
                        <div class="nav-item" onclick="scrollToSection('aop')">AOP实现</div>
                        <div class="nav-item" onclick="scrollToSection('exception')">异常处理</div>
                    </nav>
                </div>
            </div>

            <!-- Main Article -->
            <div class="lg:col-span-3">
                <!-- Introduction -->
                <section id="intro" class="content-card rounded-lg shadow-lg p-8 mb-8">
                    <p class="text-lg leading-relaxed text-gray-700 mb-6">
                        <span class="drop-cap">随</span>着业务的不断发展和系统的日益复杂，传统的单一数据库架构往往难以满足高并发、海量数据以及高可用的需求。为了应对这些挑战，分布式数据库架构逐渐成为主流。而在分布式数据库架构中，如何灵活高效地切换数据源，成为了一个关键问题。
                    </p>
                    
                    <div class="mermaid">
                        graph TB
                            A[应用程序] --> B{动态路由}
                            B --> C[主数据库]
                            B --> D[从数据库1]
                            B --> E[从数据库2]
                            C --> F[写操作]
                            D --> G[读操作]
                            E --> G
                            style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                            style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                            style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                            style D fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                            style E fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                    </div>
                    
                    <p class="text-lg leading-relaxed text-gray-700">
                        这个问题在多租户架构、读写分离、数据库分片等场景中尤为重要。通过动态数据源切换，我们能够根据不同的业务需求、请求来源或者负载情况动态选择不同的数据源，从而提升系统的性能、可扩展性和高可用性。
                    </p>
                </section>

                <!-- Basic Knowledge -->
                <section id="basics" class="content-card rounded-lg shadow-lg p-8 mb-8">
                    <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">基础知识</h2>
                    
                    <p class="text-lg leading-relaxed text-gray-700 mb-6">
                        在深入了解 <strong>Spring Boot 中实现动态数据源切换</strong> 之前，我们需要先了解一些相关的基础知识，这些知识将帮助我们理解动态数据源切换的概念和实施方法。
                    </p>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
                        <div class="feature-card">
                            <div class="flex items-center mb-4">
                                <i class="fas fa-database text-3xl text-purple-600 mr-3"></i>
                                <h3 class="text-xl font-bold">什么是数据源？</h3>
                            </div>
                            <p class="text-gray-600">
                                数据源（DataSource）是一个接口，它为 Java 程序提供数据库连接。它通常由数据库连接池实现，比如 HikariCP、C3P0 或 DBCP 等，数据源的作用就是帮助应用程序与数据库建立连接并管理这些连接。
                            </p>
                        </div>
                        
                        <div class="feature-card">
                            <div class="flex items-center mb-4">
                                <i class="fas fa-sync-alt text-3xl text-purple-600 mr-3"></i>
                                <h3 class="text-xl font-bold">动态数据源切换</h3>
                            </div>
                            <p class="text-gray-600">
                                动态数据源切换的核心思想是，根据不同的业务场景、请求类型、用户身份或者负载情况，动态地切换到不同的数据源。这种技术在多租户架构、读写分离、数据库分片等场景中尤为重要。
                            </p>
                        </div>
                    </div>

                    <div class="highlight-box">
                        <h4 class="text-xl font-bold mb-3">
                            <i class="fas fa-lightbulb mr-2"></i>应用场景
                        </h4>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div class="bg-white bg-opacity-20 rounded-lg p-4">
                                <h5 class="font-bold mb-2">多租户架构</h5>
                                <p class="text-sm">每个租户可以使用独立的数据库，或者共享一个数据库中的表，但不同的租户数据是隔离的。</p>
                            </div>
                            <div class="bg-white bg-opacity-20 rounded-lg p-4">
                                <h5 class="font-bold mb-2">读写分离</h5>
                                <p class="text-sm">在高并发的系统中，通常采用主从数据库架构，主库用于写操作，从库用于读操作。</p>
                            </div>
                            <div class="bg-white bg-opacity-20 rounded-lg p-4">
                                <h5 class="font-bold mb-2">数据库分片</h5>
                                <p class="text-sm">当单个数据库承载的数据量过大时，可以将数据分散到多个数据库（分片）。</p>
                            </div>
                        </div>
                    </div>

                    <div class="mt-8">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">
                            <i class="fas fa-cog mr-2 text-purple-600"></i>Spring Boot 默认配置
                        </h3>
                        <p class="text-gray-700 mb-4">
                            在 Spring Boot 中，默认情况下，我们通过配置文件配置数据源。Spring Boot 会自动根据这些配置创建一个单一的数据源对象：
                        </p>
                        <div class="code-block p-4" data-language="properties">
                            <pre><code>spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver</code></pre>
                        </div>
                    </div>
                </section>

                <!-- Design Approach -->
                <section id="design" class="content